package com.ts.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ts.core.Utils.RandomStringGenerator;
import com.ts.core.entity.LxbBlogger;
import com.ts.core.entity.LxbUser;
import com.ts.core.entity.Vo.IdentityVo;
import com.ts.core.mapper.LxbBloggerMapper;
import com.ts.core.mapper.LxbUserMapper;
import com.ts.core.service.LxbUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 用户基本信息 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2024-04-01
 */
@Service
public class LxbUserServiceImpl extends ServiceImpl<LxbUserMapper, LxbUser> implements LxbUserService {
    @Resource
    private LxbBloggerMapper lxbBloggerMapper;


    @Override
    public void CreateUser(String account, String password) {
        LxbUser lxbUser = new LxbUser();
        lxbUser.setAccount(account);
        lxbUser.setPassword(password);
//        //默认昵称
//        String randomString = RandomStringGenerator.generateRandomString();
//        lxbUser.setNickName(randomString);
        //默认类型是普通用户
        lxbUser.setUserType(1);
        //默认头像
        lxbUser.setHeadImg("https://ts-lxb.oss-cn-beijing.aliyuncs.com/myicon.jpeg");
        baseMapper.insert(lxbUser);


    }

    @Override
    public LxbUser GetUser(String account, String password) {
        QueryWrapper<LxbUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account",account);
        queryWrapper.eq("password",password);
        LxbUser lxbUser = baseMapper.selectOne(queryWrapper);
        return lxbUser;
    }

    @Override
    public boolean checkMobile(String mobile) {
        QueryWrapper<LxbUser> lxbUserQueryWrapper = new QueryWrapper<>();
        lxbUserQueryWrapper.eq("mobile",mobile);
        Integer integer = baseMapper.selectCount(lxbUserQueryWrapper);
        if(integer>0){
            return true;
        }
        else{
            return false;
        }


    }

    @Override
    public void identityUser(IdentityVo identityVo) {


        // 创建一个新的Blogger
        LxbBlogger lxbBlogger=new LxbBlogger();
        lxbBlogger.setIntro(identityVo.getIntro());
        lxbBlogger.setName(identityVo.getNickName());
        lxbBlogger.setCareer(identityVo.getCareer());
        lxbBlogger.setLevel(1);

        lxbBlogger.setAvatar(identityVo.getHeadImg());
        lxbBloggerMapper.insert(lxbBlogger);
        String id = lxbBlogger.getId();
        //对User表更新
        QueryWrapper<LxbUser> lxbUserQueryWrapper = new QueryWrapper<>();
        lxbUserQueryWrapper.eq("account",identityVo.getAccount());
        LxbUser lxbUser = new LxbUser();
        lxbUser.setBloggerId(id);
        lxbUser.setMobile(identityVo.getMobile());
        lxbUser.setHeadImg(identityVo.getHeadImg());
        lxbUser.setNickName(identityVo.getNickName());
        lxbUser.setName(identityVo.getName());
        lxbUser.setIdCard(identityVo.getIdCard());
        lxbUser.setBindStatus(1);
        lxbUser.setUserType(2);
        baseMapper.update(lxbUser,lxbUserQueryWrapper);



    }

    @Override
    public String getAccountByBloggerId(String bloggerId) {
        QueryWrapper<LxbUser> lxbUserQueryWrapper = new QueryWrapper<>();
        lxbUserQueryWrapper.eq("nick_name",bloggerId);

        LxbUser lxbUser = baseMapper.selectOne(lxbUserQueryWrapper);
        return lxbUser.getAccount();
    }

}
